﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace Testharness
{
    public partial class EIBDSymbolListGeneratorByTableForm : Form
    {
        public EIBDSymbolListGeneratorByTableForm()
        {
            InitializeComponent();
        }

        private void _SymbolsListBox_DragDrop(object sender, DragEventArgs e)
        {

        }

        private void _SymbolsListBox_DragEnter(object sender, DragEventArgs e)
        {

        }

        private static bool IsAllUpperCase(string word)
        {
            if (string.IsNullOrEmpty(word))
            {
                return false;
            }

            foreach (char c in word.ToCharArray())
            {
                if (!char.IsUpper(c))
                {
                    return false;
                }
            }

            return true;
        }

        private void _SymbolListGrid_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

                if (files != null && files.Length > 0)
                {
                    string filename = files[0];

                    List<string[]> rows = new List<string[]>();

                    using (StreamReader sr = new StreamReader(filename))
                    {
                        //make sure we havent reached the end
                        while (sr.Peek() >= 0)
                        {
                            //read the symbol line
                            string text = sr.ReadLine();

                            string[] items = text.Split(new char[] { ' ' });

                            string[] uppercaseWords = Regex.Split(text, @"\W");

                            string[] items2 = items.Where(i => IsAllUpperCase(i)).ToArray();

                            //string symbol = items2[items2.Length - 2];

                            

                            DataGridViewRow row = (DataGridViewRow)_SymbolListGrid.Rows[0].Clone();

                            for (int i = 0; i < items2.Length && i < 6; i++)
                            {                                 
                                row.Cells[i].Value = items2[i];
                            }

                            _SymbolListGrid.Rows.Add(row);
                        }
                    }

                    _SymbolsCountLabel.Text = string.Format("{0} symbols", _SymbolListGrid.Rows.Count);
                }
            }
            catch (Exception exception)
            {

            }
        }

        private void _SymbolListGrid_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
                // allow them to continue
                // (without this, the cursor stays a "NO" symbol
                e.Effect = DragDropEffects.All;
        }

        private void _MakeListButton_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewCell cell in _SymbolListGrid.SelectedCells)
            {
                string symbol = cell.Value.ToString();

                //does the list box contain the symbol
                if (!_SymbolsListBox.Items.Contains(symbol))
                {
                    try
                    {
                        _SymbolsListBox.Items.Add(symbol);
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.ToString());
                    }
                }
            }

            //sort the symbols
            List<string> symbolsList = new List<string>();

            foreach (string symbol in _SymbolsListBox.Items)
            {
                symbolsList.Add(symbol);
            }

            symbolsList.Sort();

            _SymbolsListBox.Items.Clear();

            foreach (string symbol in symbolsList)
            {
                _SymbolsListBox.Items.Add(symbol);
            }

            _SymbolsCountLabel.Text = _SymbolsCountLabel.Text =
                string.Format("{0} symbols", _SymbolsListBox.Items.Count);
        }

        private void _SaveFileButton_Click(object sender, EventArgs e)
        {
            if (_SaveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string location = _SaveFileDialog.FileName;

                StringBuilder stringBuilder = new StringBuilder();

                foreach (string symbol in _SymbolsListBox.Items)
                {
                    stringBuilder.AppendLine(symbol);
                }

                //write out the file symbols
                File.WriteAllText(location, stringBuilder.ToString());
            }
        }

        private void _ClearListButton_Click(object sender, EventArgs e)
        {
            _SymbolListGrid.Rows.Clear();
            _SymbolsListBox.Items.Clear();
        }
    }
}
